package com.example.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import com.example.dao.ArticleMapper;
import com.example.entity.Article;

import org.apache.ibatis.session.SqlSession;

public class DataTools {

    public static void main(String[] args) {
        insertRecords(20000);
    }

    public static void insertRecords(int n){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        ArrayList<Article> articles = new ArrayList<Article>();
        Random r = new Random();
        // article(title,author_pk,content)
        for (int i = 0; i < n; i++) {
            Article article = new Article();
            article.setTitle("title_"+i);
            article.setAuthor_pk(r.nextInt(16)+1);
            article.setContent("content_"+i);
            articles.add(article);
        }

        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("articles", articles);

        mapper.addArticle2(hashMap);

        sqlSession.commit();
        sqlSession.close();
    }
}
